#!/usr/bin/env python
import qb_database
import time
import config
import datetime
import collections
import sys
import qb
q = qb_database.QubeDatabase()


seq_running = {}
seq_running_list = []
seq_pending = {}
seq_pending_list = []
seq_complete = {}
seq_complete_list = []
seq_complete_time = {}
seq_complete_time_list = []
seq_complete_time_list_fiter = []
seq_complete_all = []
list_per_subjob = []
list_per_complete = []
list_per_subjob_dict = {}
list_per_complete_dict = {}
list_per_complete_BTY = []
list_per_complete_VOL =[]
list_per_complete_MR = []
list_per_complete_Nuke = []
list_per_complete_Other = []
list_per_complete_BTY_dic= {}
list_per_complete_BTY_r0_dic= {}
list_per_complete_BTY_r1_dic= {}
list_per_complete_BTY_r3_dic= {}
list_per_complete_VOL_dic ={}
list_per_complete_MR_dic = {}
list_per_complete_Nuke_dic = {}
list_per_complete_Other_dic = {}
list_per_subjob_BTY = []
list_per_subjob_BTY_r1 = []
list_per_subjob_BTY_r3 = []
list_per_subjob_BTY_r0 = []
list_per_subjob_VOL = []
list_per_subjob_MR = []
list_per_subjob_Nuke = []
list_per_subjob_Other = []
list_per_subjob_BTY_dic = {}
list_per_subjob_BTY_r1_dic = {}
list_per_subjob_BTY_r3_dic = {}
list_per_subjob_BTY_r0_dic = {}
list_per_subjob_VOL_dic = {}
list_per_subjob_MR_dic = {}
list_per_subjob_Nuke_dic = {}
list_per_subjob_Other_dic = {}

time_use_all_list = []
time_use_all_list_BTY = []
time_use_all_list_BTY_r0 = []
time_use_all_list_BTY_r1 = []
time_use_all_list_BTY_r3 = []
time_use_all_list_VOL = []
time_use_all_list_MR = []
time_use_all_list_Nuke = []
time_use_all_list_Other = []
# TODO sort list
def muti_sort(lists):
    '''sorted by list element's first element

    :param lists:like this [[5,2,3],[2,3,4],[4,6,8],...]
    :return:[[2,3,4],[4,6,8],[5,2,3]]
    '''

    assert isinstance(lists,list)
    first_ele = lambda s: s[0]
    second_ele = lambda s: s[1]
    last_ele = lambda s: s[-1]
    a = sorted(lists,key = last_ele)
    b = sorted(a,key = second_ele)
    c = sorted(b,key = first_ele)
    return c


def dic_make(list_name,dict_name):
    '''make dict to list

    :param list_name:[[key,int],...] list must have key and value like [['shot1',20],['shot2',30],['shot1',40]]
    :param dict_name: just a name to return
    :return: {'shot1':60,'shot2':30}
    '''
    for i in list_name :
        if not dict_name.has_key(i[0]):
            dict_name[i[0]] = 0
        dict_name[i[0]] += i[1]
    return dict_name


def dic_make_onelist(list_name,dict_name):
    if not dict_name.has_key(list_name[0]):
        dict_name[list_name[0]] = 0
    dict_name[list_name[0]] += list_name[1]
    return dict_name


def pasted_time(t):
    '''
    :param t: seconds
    :return: h:m:s
    '''
    if t == '':
        return ''
    s = t % 60
    m = t / 60 % 60
    h = t / 3600
    return '%02d:%02d:%02d' % (h, m, s)


def calaverage(list_time_s):
    '''
    :param list_time_s: a list with int
    :return: a average number
    '''
    if len(list_time_s) !=0:
        num = 0
        for i in range(len(list_time_s)):
            num += list_time_s[i]
        calaverage = num/len(list_time_s)
    else:
        calaverage = 0
    return calaverage


def Qube_time_use(timestart,timecomplete):
    timestart = timestart + config.QUBE_TIME
    timecomplete = timecomplete + config.QUBE_TIME
    time_start = datetime.datetime.fromtimestamp(timestart)
    time_complete = datetime.datetime.fromtimestamp(timecomplete)
    time_use = (time_complete - time_start).total_seconds()
    return time_use


def time_subjob(timestart_qube_sta,timestart_qube_end):
    '''the number of subjob and complete subjob

    :param timestart_qube_sta: timesubmit
    :param timestart_qube_end: time_end
    :return: two dict .one is {key:datetime value:the number of subjob},one is {key:datetime value:the number of complete subjob}
    '''
    output = sys.stdout
    count = 0
    job = q.job(
        query_field='id,prod_show,prod_shot,prod_seq,status,timesubmit,timestart,timeout,timecomplete,todo,todotally,name',
        query='(status != 261) and (status != 48) and (timesubmit between %s and %s)' % (timestart_qube_sta, timestart_qube_end))
    job_len = len(job)
    for i in job:

        count += 1
        output.write('\r\033[22;34m Progress: %.0f%% \033[0m'%(count/(job_len/100)))

        timesubmit = time.strftime('%Y-%m-%d', time.localtime(i['timesubmit'] + config.QUBE_TIME))
        list_per_subjob.append([timesubmit, i['todo']])
        if ('BTY' in i['name']) and ('Denoise' not in i['name']):
            list_per_subjob_BTY.append([timesubmit, i['todo']])
            if ('r1' in i['name']):
                list_per_subjob_BTY_r1.append([timesubmit, i['todo']])
            if ('r3' in i['name']):
                list_per_subjob_BTY_r3.append([timesubmit, i['todo']])
            if ('r0' in i['name']):
                list_per_subjob_BTY_r0.append([timesubmit, i['todo']])
        elif ('VOL' in i['name']) and ('Denoise' not in i['name']):
            list_per_subjob_VOL.append([timesubmit, i['todo']])
        elif 'MR' in i['name']:
            list_per_subjob_MR.append([timesubmit, i['todo']])
        elif 'Nuke' in i['name']:
            list_per_subjob_Nuke.append([timesubmit, i['todo']])
        else:
            list_per_subjob_Other.append([timesubmit, i['todo']])

        job_id = i['id']
        subjob = q.agenda(job_id, query_field='status,timestart,timecomplete,timecumulative,name')
        for frame in subjob:
            if frame['status'] == 16:
                time_use_s = Qube_time_use(frame['timestart'],frame['timecomplete'])
                # time_use_s = frame['timecumulative'] data is wrong
                time_use_all_list.append(time_use_s)
                time_complete = time.strftime('%Y-%m-%d', time.localtime(frame['timecomplete'] + config.QUBE_TIME))
                dic_make_onelist([time_complete,1],list_per_complete_dict)
                if ('BTY' in i['name']) and ('Denoise' not in i['name']):
                    dic_make_onelist([time_complete, 1], list_per_complete_BTY_dic)
                    time_use_all_list_BTY.append([time_use_s,'%s:%s'%(i['id'],frame['name'])])
                    if ('r1' in i['name']):
                        dic_make_onelist([time_complete, 1], list_per_complete_BTY_r1_dic)
                        time_use_all_list_BTY_r1.append([time_use_s, '%s:%s' % (i['id'], frame['name']),i['prod_seq']])
                    if ('r3' in i['name']):
                        dic_make_onelist([time_complete, 1], list_per_complete_BTY_r3_dic)
                        time_use_all_list_BTY_r3.append([time_use_s, '%s:%s' % (i['id'], frame['name']),i['prod_seq']])
                    if ('r0' in i['name']):
                        dic_make_onelist([time_complete, 1], list_per_complete_BTY_r0_dic)
                        time_use_all_list_BTY_r0.append([time_use_s, '%s:%s' % (i['id'], frame['name']),i['prod_seq']])
                elif ('VOL' in i['name']) and ('Denoise' not in i['name']):
                    dic_make_onelist([time_complete, 1], list_per_complete_VOL_dic)
                    time_use_all_list_VOL.append([time_use_s,'%s:%s'%(i['id'],frame['name'])])
                elif 'MR' in i['name']:
                    dic_make_onelist([time_complete, 1], list_per_complete_MR_dic)
                    time_use_all_list_MR.append(time_use_s)
                elif 'Nuke' in i['name']:
                    dic_make_onelist([time_complete, 1], list_per_complete_Nuke_dic)
                    time_use_all_list_Nuke.append(time_use_s)
                else:
                    dic_make_onelist([time_complete, 1], list_per_complete_Other_dic)
                    time_use_all_list_Other.append(time_use_s)

    output.flush()
    dic_make(list_per_subjob,list_per_subjob_dict)
    dic_make(list_per_subjob_BTY,list_per_subjob_BTY_dic)
    dic_make(list_per_subjob_BTY_r1,list_per_subjob_BTY_r1_dic)
    dic_make(list_per_subjob_BTY_r3, list_per_subjob_BTY_r3_dic)
    dic_make(list_per_subjob_BTY_r0, list_per_subjob_BTY_r0_dic)
    dic_make(list_per_subjob_VOL,list_per_subjob_VOL_dic)
    dic_make(list_per_subjob_MR,list_per_subjob_MR_dic)
    dic_make(list_per_subjob_Nuke,list_per_subjob_Nuke_dic)
    dic_make(list_per_subjob_Other,list_per_subjob_Other_dic)

    for key in list_per_subjob_dict:
        if (key not in list_per_subjob_BTY_dic) :
            list_per_subjob_BTY_dic[key] = 0
        if (key not in list_per_complete_BTY_dic) :
            list_per_complete_BTY_dic[key] = 0
        if (key not in list_per_subjob_BTY_r1_dic) :
            list_per_subjob_BTY_r1_dic[key] = 0
        if (key not in list_per_complete_BTY_r1_dic) :
            list_per_complete_BTY_r1_dic[key] = 0
        if (key not in list_per_subjob_BTY_r3_dic) :
            list_per_subjob_BTY_r3_dic[key] = 0
        if (key not in list_per_complete_BTY_r3_dic) :
            list_per_complete_BTY_r3_dic[key] = 0
        if (key not in list_per_subjob_BTY_r0_dic) :
            list_per_subjob_BTY_r0_dic[key] = 0
        if (key not in list_per_complete_BTY_r0_dic) :
            list_per_complete_BTY_r0_dic[key] = 0
        if (key not in list_per_subjob_VOL_dic) :
            list_per_subjob_VOL_dic[key] = 0
        if (key not in list_per_complete_VOL_dic) :
            list_per_complete_VOL_dic[key] = 0
        if (key not in list_per_subjob_MR_dic) :
            list_per_subjob_MR_dic[key] = 0
        if (key not in list_per_complete_MR_dic) :
            list_per_complete_MR_dic[key] = 0
        if (key not in list_per_subjob_Nuke_dic) :
            list_per_subjob_Nuke_dic[key] = 0
        if (key not in list_per_complete_Nuke_dic) :
            list_per_complete_Nuke_dic[key] = 0
        if (key not in list_per_subjob_Other_dic) :
            list_per_subjob_Other_dic[key] = 0
        if (key not in list_per_complete_Other_dic) :
            list_per_complete_Other_dic[key] = 0


    return list_per_subjob_dict,list_per_complete_dict,\
           list_per_complete_BTY_dic,list_per_complete_VOL_dic,list_per_complete_MR_dic,list_per_complete_Nuke_dic,list_per_complete_Other_dic,\
           list_per_subjob_BTY_dic,list_per_subjob_VOL_dic,list_per_subjob_MR_dic,list_per_subjob_Nuke_dic,list_per_subjob_Other_dic, \
           time_use_all_list,time_use_all_list_BTY,time_use_all_list_VOL,time_use_all_list_MR,time_use_all_list_Nuke,time_use_all_list_Other, \
           time_use_all_list_BTY_r1,time_use_all_list_BTY_r3,time_use_all_list_BTY_r0,\
           list_per_subjob_BTY_r1_dic,list_per_subjob_BTY_r3_dic,list_per_subjob_BTY_r0_dic, \
           list_per_complete_BTY_r1_dic,list_per_complete_BTY_r3_dic,list_per_complete_BTY_r0_dic


def get_subjob_and_complete_time(timestart_qube_sta,timestart_qube_end,veiw_shot_seq):
    '''to get some data

    :param timestart_qube_sta: submit_time like '2019-01-10 15:00:00'
    :param timestart_qube_end: end_time
    :param veiw_shot_seq: 'shot' or 'sequence'
    :return: allComplete,allRunning,allPending,seq or shot complete's time(no filter),seq or shot's (complete/pending/running) subjob.
    '''

    all_subjob = 0
    all_r = 0
    all_p = 0
    all_c = 0

    for i in q.job(query_field='id,prod_show,prod_shot,prod_seq,status,timesubmit,timestart,timeout,timecomplete,todo,todotally',query='timesubmit between %s and %s' % (timestart_qube_sta, timestart_qube_end)):

        all_subjob += int(i['todo'])
        seq_name = map(str, i['prod_seq'].split('_'))
        shot_name = i['prod_shot']
        if veiw_shot_seq == 'shot':
            name = shot_name
        if veiw_shot_seq == 'sequence':
            name = seq_name[0]
        if i['status'] == 336:
            seq_todotally = map(int, i['todotally'].split())
            if name == '':
                name = "No_Assigned"
            seq_running_list.append([name, seq_todotally[0]])
            all_r += seq_todotally[0]
        if (i['status'] == 336) or (i['status'] == 320) or (i['status'] == 272):
            seq_todotally = map(int, i['todotally'].split())
            if name == '':
                name = "No_Assigned"
            if seq_todotally[3] != 0:
                seq_pending_list.append([name, seq_todotally[3]])
                all_p += seq_todotally[3]
        if i['status'] == 16:
            seq_todotally = map(int, i['todotally'].split())
            if name == '':
                name = "No_Assigned"
            seq_complete_list.append([name, seq_todotally[2]])
            all_c += seq_todotally[2]
            seq_complete_time_list.append([name, i['timecomplete']])
    return all_subjob,all_r,all_p,all_c,seq_complete_time_list,seq_complete_list,seq_pending_list,seq_running_list


def make_complete_time_str_dict(timestart_qube_sta,timestart_qube_end,veiw_shot_seq):
    '''

    :param timestart_qube_sta: submit_time like '2019-01-10 15:00:00'
    :param timestart_qube_end:
    :param veiw_shot_seq: 'shot' or 'sequence'
    :return: the seq or shot complete all and complete time
    '''
    all_subjob, all_r, all_p, all_c, seq_complete_time_list, seq_complete_list, seq_pending_list, seq_running_list = get_subjob_and_complete_time(timestart_qube_sta,timestart_qube_end,veiw_shot_seq)

    for i in seq_complete_time_list:

        if not seq_complete_time.has_key(i[0]):
            seq_complete_time[i[0]] = []
        seq_complete_time[i[0]].append(i[1])
    for key in seq_complete_time.keys():
        # get the end time with the job from shot
        value = max(seq_complete_time[key]) + config.QUBE_TIME
        value_conver = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(value))
        seq_complete_time[key] = value_conver

    dic_make(seq_running_list, seq_running)
    dic_make(seq_pending_list, seq_pending)
    dic_make(seq_complete_list, seq_complete)

    for key in seq_complete:
        if (key not in seq_running) and (key not in seq_pending):
            seq_complete_all.append(key)  # get the complete name seq or shot .
    for key in seq_complete_time:
        if (key not in seq_running) and (key not in seq_pending):
            seq_complete_time_list_fiter.append([key, seq_complete_time[key]])  # get the complete time.
    return seq_complete_all,seq_complete_time_list_fiter





def main(timestart,timeend):

    timestart_arr_sta = time.strptime(timestart, "%Y-%m-%d %H:%M:%S")
    time_start_local = time.localtime(time.mktime(timestart_arr_sta))
    timestart_qube_sta = int(time.mktime(timestart_arr_sta)) - config.QUBE_TIME
    timestart_arr_end = time.strptime(timeend, "%Y-%m-%d %H:%M:%S")
    time_end_local = time.localtime(time.mktime(timestart_arr_end))
    timestart_qube_end = int(time.mktime(timestart_arr_end)) - config.QUBE_TIME
    d1 = datetime.datetime(time_start_local.tm_year, time_start_local.tm_mon, time_start_local.tm_mday,
                           time_start_local.tm_hour)
    d2 = datetime.datetime(time_end_local.tm_year, time_end_local.tm_mon, time_end_local.tm_mday,
                           time_end_local.tm_hour)
    days = d2 - d1
    the_number_day = days.days
    if the_number_day <= 0:
        the_number_day = 1
    veiw_shot_seq = 'sequence'
    all_subjob, all_r, all_p, all_c, seq_complete_time_list, seq_complete_list, seq_pending_list, seq_running_list = get_subjob_and_complete_time(timestart_qube_sta,timestart_qube_end,veiw_shot_seq)
    seq_complete_all, seq_complete_time_list_fiter = make_complete_time_str_dict(timestart_qube_sta,timestart_qube_end,veiw_shot_seq)

    dict_date_subjob, dict_date_complete, \
    list_per_complete_BTY_dic, list_per_complete_VOL_dic, list_per_complete_MR_dic, list_per_complete_Nuke_dic, list_per_complete_Other_dic, \
    list_per_subjob_BTY_dic, list_per_subjob_VOL_dic, list_per_subjob_MR_dic, list_per_subjob_Nuke_dic, list_per_subjob_Other_dic, \
    t_all, t_B, t_V, t_M, t_N, t_O, \
    t_B_r1,t_B_r3,t_B_r0,\
    list_per_subjob_BTY_r1_dic,list_per_subjob_BTY_r3_dic,list_per_subjob_BTY_r0_dic, \
    list_per_complete_BTY_r1_dic,list_per_complete_BTY_r3_dic,list_per_complete_BTY_r0_dic = time_subjob(timestart_qube_sta, timestart_qube_end)
    all_time_average = pasted_time(calaverage(t_all))
    BTY_time_average = pasted_time(calaverage([i[0] for i in t_B]))
    BTY_r1_time_average = pasted_time(calaverage([i[0] for i in t_B_r1]))
    BTY_r3_time_average = pasted_time(calaverage([i[0] for i in t_B_r3]))
    BTY_r0_time_average = pasted_time(calaverage([i[0] for i in t_B_r0]))
    VOL_time_average = pasted_time(calaverage([i[0] for i in t_V]))
    MR_time_average = pasted_time(calaverage(t_M))
    Nuke_time_average = pasted_time(calaverage(t_N))
    O_time_average = pasted_time(calaverage(t_O))
    if len(t_B) != 0:
        max_time = max([i[0] for i in t_B])
        min_time = min([j[0] for j in t_B])
        BTY_peak_time = [pasted_time(max_time), pasted_time(min_time)]
        for i in t_B:
            if i[0] == max_time:
                BTY_peak_time.append(i[1])
    else:
        BTY_peak_time = ['None','None','None']


    seq_dict_r1 = {}
    seq_dict_r3 = {}
    seq_dict_r0 = {}
    if len(t_B_r1) != 0:
        for n in t_B_r1:
            if not seq_dict_r1.has_key(n[2]):
                seq_dict_r1[n[2]] = 0
            seq_dict_r1[n[2]] += n[0]
        seq_name_list = [k[2] for k in t_B_r1]
        seq_c_subjob = collections.Counter(seq_name_list)

        for key,value in seq_c_subjob.items():
            if key in seq_dict_r1:
                all = seq_dict_r1[key]
                num = value
                temp = pasted_time(all/num)
                seq_dict_r1[key] = temp

        max_time = max([i[0] for i in t_B_r1])  # time use i[0]
        min_time = min([j[0] for j in t_B_r1])
        BTY_r1_peak_time = [pasted_time(max_time), pasted_time(min_time)]
        for i in t_B_r1:
            if i[0] == max_time:
                BTY_r1_peak_time.append(i[1])
    else:
        BTY_r1_peak_time = ['None','None','None']

    if len(t_B_r3) != 0:
        for n in t_B_r3:
            if not seq_dict_r3.has_key(n[2]):
                seq_dict_r3[n[2]] = 0
            seq_dict_r3[n[2]] += n[0]
        seq_name_list_r3 = [k[2] for k in t_B_r3]
        seq_c_subjob_r3 = collections.Counter(seq_name_list_r3)

        for key,value in seq_c_subjob_r3.items():
            if key in seq_dict_r3:
                all = seq_dict_r3[key]
                num = value
                temp = pasted_time(all/num)
                seq_dict_r3[key] = temp

        max_time = max([i[0] for i in t_B_r3])
        min_time = min([j[0] for j in t_B_r3])
        BTY_r3_peak_time = [pasted_time(max_time), pasted_time(min_time)]
        for i in t_B_r3:
            if i[0] == max_time:
                BTY_r3_peak_time.append(i[1])
    else:
        BTY_r3_peak_time = ['None','None','None']

    if len(t_B_r0) != 0:
        max_time = max([i[0] for i in t_B_r0])
        min_time = min([j[0] for j in t_B_r0])
        for n in t_B_r0:
            if not seq_dict_r0.has_key(n[2]):
                seq_dict_r0[n[2]] = 0
            seq_dict_r0[n[2]] += n[0]
        seq_name_list_r0 = [k[2] for k in t_B_r0]
        seq_c_subjob_r0 = collections.Counter(seq_name_list_r0)

        for key, value in seq_c_subjob_r0.items():
            if key in seq_dict_r0:
                all = seq_dict_r0[key]
                num = value
                temp = pasted_time(all / num)
                seq_dict_r0[key] = temp
        BTY_r0_peak_time = [pasted_time(max_time), pasted_time(min_time)]
        for i in t_B_r0:
            if i[0] == max_time:
                BTY_r0_peak_time.append(i[1])
    else:
        BTY_r0_peak_time = ['None','None','None']

    if len(t_V) != 0:
        max_time_v = max([i[0] for i in t_V])
        min_time_v = min([j[0] for j in t_V])
        VOL_peak_time = [pasted_time(max_time_v), pasted_time(min_time_v)]
        for j in t_V:
            if j[0] == max_time_v:
                VOL_peak_time.append(j[1])
    else:
        VOL_peak_time = ['None','None','None']
    print '\n'
    print '\n'
    print '                                \033[1;36m Subjob Per day info\033[0m                                    '
    print '---------------------------------------------------------------------------------------------------------------------------'
    print '\033[1;35m Time:\033[0m \033[1;32m %s to %s\033[0m      |       \033[1;35m day:\033[0m \033[1;32m %s\033[0m   ' % (
        timestart[:10], timeend[:10], the_number_day)
    print '---------------------------------------------------------------------------------------------------------------------------'
    print '\033[1;35m all_subjob:\033[0m %s           \033[1;35m all_subjob_complete:\033[0m %s    ' % (
        all_subjob, all_c)
    print '---------------------------------------------------------------------------------------------------------------------------'
    print '\033[1;35m subjob_per_day :\033[0m %s       \033[1;35m subjob_complete_per_day:\033[0m %s        \033[1;35m time_per_subjob:\033[0m %s  ' % (
        all_subjob / the_number_day, all_c / the_number_day, all_time_average)
    print '---------------------------------------------------------------------------------------------------------------------------'
    items1 = dict_date_subjob.items()
    items2 = dict_date_complete.items()
    items1.sort()
    items2.sort()
    for key, values in items1:
        if key in dict_date_complete:
            print '\033[1;35m Date:\033[0m %s          \033[1;35m subjob:\033[0m %s          \033[1;35m subjob_complete:\033[0m %s' % (
                key, dict_date_subjob[key], dict_date_complete[key])

    print '---------------------------------------------------------------------------------------------------------------------------'
    print '\033[1;36m The Number of different type subjob:\033[0m \n'
    items1 = dict_date_subjob.items()
    items1.sort()
    for key, values in items1:
        if key in dict_date_subjob:
            print '\033[1;35m Date:\033[0m %s    \033[1;35m BTY_subjob\complete:\033[0m %s\%s      ' % (
            key, list_per_subjob_BTY_dic[key], list_per_complete_BTY_dic[key])

            print '                     \033[1;35m BTY_r1_subjob\complete:\033[0m %s\%s     \033[1;35m BTY_r3_subjob\complete:\033[0m %s\%s     \033[1;35m BTY_r0_subjob\complete:\033[0m %s\%s'% (
                list_per_subjob_BTY_r1_dic[key], list_per_complete_BTY_r1_dic[key],list_per_subjob_BTY_r3_dic[key],
                list_per_complete_BTY_r3_dic[key],list_per_subjob_BTY_r0_dic[key], list_per_complete_BTY_r0_dic[key])
            print '                     \033[1;35m VOL_subjob\complete:\033[0m %s\%s         \033[1;35m MR_subjob\complete:\033[0m %s\%s'%(list_per_subjob_VOL_dic[key],list_per_complete_VOL_dic[key],list_per_subjob_MR_dic[key], list_per_complete_MR_dic[key])
            print '                     \033[1;35m Nuke_subjob\complete:\033[0m %s\%s        \033[1;35m Other_subjob\complete:\033[0m %s\%s\n' % (list_per_subjob_Nuke_dic[key],list_per_complete_Nuke_dic[key],list_per_subjob_Other_dic[key], list_per_complete_Other_dic[key])


    print '---------------------------------------------------------------------------------------------------------------------------'
    print '\033[1;36m The Time of different type job:\033[0m \n'
    print '\033[1;35m BTY_Average_time:\033[0m %s '%BTY_time_average
    print '\033[1;35m BTY_r1_Average_time:\033[0m %s     \033[1;35m BTY_r3_Average_time:\033[0m %s     \033[1;35m BTY_r0_Average_time:\033[0m %s'%(BTY_r1_time_average,BTY_r3_time_average,BTY_r0_time_average)
    print '\033[1;35m VOL_Average_time:\033[0m %s'%VOL_time_average
    print '\033[1;35m MR_Average_time:\033[0m %s        \033[1;35m Nuke_Average_time:\033[0m %s'%(MR_time_average,Nuke_time_average)
    print '\033[1;35m Other_Average_time:\033[0m %s \n'%(O_time_average)
    print '\033[22;32m BTY_Peak_time:\033[0m %s  \033[22;32m Job_ID:\033[0m %s' % (BTY_peak_time[0], BTY_peak_time[2])
    print '\033[22;32m BTY_r1_Peak_time:\033[0m %s    \033[22;32m Job_ID:\033[0m %s ' % (
    BTY_r1_peak_time[0], BTY_r1_peak_time[2])
    print '\033[22;32m BTY_r3_Peak_time:\033[0m %s  \033[22;32m Job_ID:\033[0m %s ' % (
    BTY_r3_peak_time[0], BTY_r3_peak_time[2])
    print '\033[22;32m BTY_r0_Peak_time:\033[0m %s  \033[22;32m Job_ID:\033[0m %s ' % (
    BTY_r0_peak_time[0], BTY_r0_peak_time[2])
    print '\033[22;32m VOL_Peak_time:\033[0m %s   \033[22;32m Job_ID:\033[0m %s  ' % (VOL_peak_time[0], VOL_peak_time[2])
    print '\n'
    print '\n'





    print '                                 \033[1;36m Sequence info\033[0m                                    '
    print '---------------------------------------------------------------------------------------------------------------------------'
    print '\033[1;35m Time:\033[0m \033[1;32m %s to %s\033[0m      |       \033[1;35m day:\033[0m \033[1;32m %s\033[0m   ' % (
    timestart[:10], timeend[:10], days.days)
    print '---------------------------------------------------------------------------------------------------------------------------'
    print '\033[1;35m Sequence_Complete(%s):\033[0m' % len(seq_complete_all), seq_complete_all
    print '---------------------------------------------------------------------------------------------------------------------------'
    print '\033[1;35m Sequence_Complete_time:\033[0m'
    seq_complete_time_list_fiter_sort = muti_sort(seq_complete_time_list_fiter)
    for i in seq_complete_time_list_fiter_sort:
        print '        ', i
    print '---------------------------------------------------------------------------------------------------------------------------'
    print '\033[1;35m Sequence(running_subjob[%s]):\033[0m' % all_r, seq_running
    print '---------------------------------------------------------------------------------------------------------------------------'
    print '\033[1;35m Sequence(pending_subjob[%s]):\033[0m' % all_p, seq_pending
    print '---------------------------------------------------------------------------------------------------------------------------'
    print '\033[1;35m Sequence(complete_subjob[%s]):\033[0m' % all_c
    items_seq = seq_complete.items()
    items_seq.sort()
    for key, value in items_seq:
        if key in seq_dict_r1:
            # if key in seq_dict_r0:
            #     print '         Sequence : %s    complete : %s   |  r1-avg-time: %s     r0-avg-time: %s' % (key, value, seq_dict_r1[key],seq_dict_r0[key])
            # else:
            #     print '         Sequence : %s    complete : %s   |  r1-avg-time: %s' % (key, value, seq_dict_r1[key])
            if key in seq_dict_r3:
                print '         Sequence : %s    complete : %s   |  r1-avg-time: %s    r3-avg-time: %s' % (key, value, seq_dict_r1[key],seq_dict_r3[key])
            else:
                print '         Sequence : %s    complete : %s   |  r1-avg-time: %s' % (key, value, seq_dict_r1[key])
        else:
            print '         Sequence : %s    complete : %s    ' % (key, value)


if __name__ == "__main__":
    def re_dying_job():
        job_dying = q.job(query_field='id,status,todo,name', query='status = 261 or todo = 0')
        for i in job_dying:
            print i['id'], i['status'], i['name'], i['todo']
            print qb.remove(i['id'])

    def validate(date_text):
        try:
            if date_text != datetime.datetime.strptime(date_text, '%Y-%m-%d %H:%M:%S').strftime('%Y-%m-%d %H:%M:%S'):
                raise ValueError
            return True
        except ValueError:
            print '\033[22;31m Time format error ,try again:\033[0m'
            return False
    timeend_default = time.strftime( "%Y-%m-%d %H:%M:%S",time.localtime())
    time_start = datetime.date.today() - datetime.timedelta(days=7)
    time_start_stamo = time.strptime(str(time_start) + ' 00:00:00', "%Y-%m-%d %H:%M:%S")
    timestart_default = time.strftime("%Y-%m-%d %H:%M:%S",time_start_stamo)

    timestart = raw_input('Plese input the Date of start (Exam:\033[22;36m %s \033[0m) :'%timestart_default)
    if timestart == '':
        timestart = timestart_default
        print '\033[22;36m %s \033[0m'%timestart
    while validate(timestart) == False:
        timestart = raw_input('Plese input the Date of start (Exam:\033[22;36m %s \033[0m) :' % timestart_default)
    timeend = raw_input('Plese input the Date of end (Exam:\033[22;36m %s \033[0m) :' % timeend_default)
    if timeend == '':
        timeend = timeend_default
        print '\033[22;36m %s \033[0m' % timeend
    while validate(timeend) == False:
        timeend = raw_input('Plese input the Date of end (Exam:\033[22;36m %s \033[0m) :' % timeend_default)
    print '\033[22;34m Oops! I think I need some times to collect data .\033[0m'
    try:
        main(timestart, timeend)
    except:
        re_dying_job()
        main(timestart, timeend)
